<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$return=array();
$worker = pdo_get('jy_peimilk_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
if(!$worker['id'] || $worker['status'] !=1){
	exi('','error','/apply/');
}
if($_RQ['op']=='main'){
	$sendtypes=array(
		'1'=>'同城配送',
		'2'=>'到店自取',
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0);
	if($_RQ['merchid']){
		$this->session('merchid',$_RQ['merchid']);
		$groupspram['merchid']=$_RQ['merchid'];
		$return['merch']=pdo_get('jy_merch_user',array('id'=>$_RQ['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','address','lat_lng'));
		$sendtypes=model('jy_merch/model')->sendtypes($_RQ['merchid'],$sendtypes);
		if(count($sendtypes)==1){
			$sendtype=$sendtypes[1]?1:2;
		}
	}else{
		$groupspram['mid']=0;
	}
	if($parentid){
		$return['pid']=0;
	}
	$category=pdo_getall('jy_weishop_goods_group',$groupspram,array(),'','paixu desc');
	$return['category']=$category;
	$return['post']=array(
		'merchid'=>$_RQ['merchid'],
		'sendtype'=>$sendtype?:1,
		'paytype'=>1,
		'goods'=>array(),
		'peiset'=>array('days'=>'','goods'=>array('k'=>0)),
	);
	if($_RQ['merchid']){
		$return['yuesetting']=$this->plugin_setting('yuesetting'.$_RQ['merchid']);
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi'.$_RQ['merchid']);
	}else{
		$return['yuesetting']=$this->plugin_setting('yuesetting');
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi');
	}
	$weekarray=array('日','一','二','三','四','五','六');
	$no_weeks=$return['yuesetting']['no_weeks'];
	foreach($weekarray as $k=>$v){
		if(strpos($no_weeks,$k.';') !== false){
			$isno=1;
		}else{
			$isno=0;
		}
		$work_weekarray[]=array(
			'id'=>$k,
			'title'=>$v,
			'isno'=>$isno,
		);
	}
	$return['params']=array(
		'pid'=>'-1',
		'merchid'=>$_RQ['merchid'],
		'nowmonth'=>date('Y-m'),
		'work_weekarray'=>$work_weekarray,
	);
	$merchs=pdo_getall('jy_merch_user',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','address','lat_lng'));	
	foreach($merchs as &$mv){
		$mv['lat_lng']=json_decode($mv['lat_lng'],true);
		$mv['sendtypes']=model('jy_merch/model')->sendtypes($mv['id'],$sendtypes);
		if(count($mv['sendtypes'])==1){
			$mv['sendtype']=$mv['sendtypes'][1]?1:2;
		}else{
			$mv['sendtype']=1;
		}
	}
	$return['merchs']=$merchs;
	$return['sendtypes']=$sendtypes;
	//设置时间
	$return['currentyear'] =$_RQ['year']?:date('Y');
	$return['currentmonth'] = $_RQ['month']?:date('m');
	$this->session('upgoods','');
	$this->session('peisetgoods','');
	exi($return);
}
if($_RQ['op']=='getcategory'){
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0);
	if($_RQ['merchid']){
		$groupspram['merchid']=$_RQ['merchid'];
		$this->session('merchid',$_RQ['merchid']);
	}else{
		$groupspram['mid']=0;
	}
	if($parentid){
		$return['pid']=0;
	}
	$category=pdo_getall('jy_weishop_goods_group',$groupspram,array(),'','paixu desc');
	$return['list']=$category;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mobile'] || !$postdata['realname']){
			exi('请填写客户姓名和电话！','error');
	}
	if(!$postdata['mid']){
			exi('请填写客户会员ID！','error');
	}
	if(!$postdata['pcd'] || !$postdata['address']){
			exi('请填写地址！','error');
	}
		
	if(!$postdata['goods']){
			exi('请选择商品！','error');
	}
	$address=array(
			'pcd'=>$postdata['pcd'],
			'address'=>$postdata['address'],
			'mobile'=>$postdata['mobile'],
			'realname'=>$postdata['realname'],
	);
	$postdata['peiset']['goods']=$this->session('peisetgoods');
	if(!$postdata['peiset']['times']){
		exi('请先设置预约配送的时间段！','error');
	}
	if(!$postdata['peiset']['goods']){
		exi('请先设置预约配送商品的规律！','error');
	}
	$new_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$postdata['mid'],
			'merchid'=>$postdata['merchid'],
			'goodsprice'=>$postdata['goodsprice'],
			'price'=>$postdata['price'],
			'dispatchprice'=>$postdata['dispatchprice'],
			'remark'=>trim($postdata['remark']),
			'goods'=>ijson_encode($postdata['goods']),
			'sendtype'=>$postdata['sendtype']?:1,
			'paytype'=>$postdata['paytype']?:1,
			'mobile'=>$postdata['mobile'],
			'realname'=>$postdata['realname'],
			'address'=>ijson_encode($address),
			'peisong_id'=>$worker['id'],
			'peiset'=>ijson_encode($postdata['peiset']),
			'createtime'=>time(),
	);
	$this->insert('baodan',$new_order);
	exi('提交成功','','/index/');
}
if($_RQ['op']=='upgoods'){
	$type=$_RQ['type'];
	$goods=get_postdata();
	$allgoods=$this->session('upgoods')?:array();
	$goods['options']['id']=$goods['options']['id']?:0;
	$key=$goods['id'].'_'.$goods['options']['id'];
	if($type=='sp_plus' || $type=='plus'){
			$leixings=$goods['leixings'];
			if(!count($leixings)){
				$leixings[]=array(
					'title'=>'默认类型',
					'data'=>array(
						array('id'=>0,'num'=>''),
						array('id'=>1,'num'=>''),
						array('id'=>2,'num'=>''),
						array('id'=>3,'num'=>''),
						array('id'=>4,'num'=>''),
						array('id'=>5,'num'=>''),
						array('id'=>6,'num'=>''),
					)
				);
			}
			foreach($leixings as &$lv){
				$lv['data']=array(
					array('id'=>0,'num'=>''),
					array('id'=>1,'num'=>''),
					array('id'=>2,'num'=>''),
					array('id'=>3,'num'=>''),
					array('id'=>4,'num'=>''),
					array('id'=>5,'num'=>''),
					array('id'=>6,'num'=>''),
				);
			}
			$numset=$leixings;	
	}
	if($type=='sp_plus'){
		if($allgoods[$key]){
			$allgoods[$key]['total']++;
		}else{
			$newgoods['id']=$goods['id'];
			$newgoods['goodsid']=$goods['id'];
			$newgoods['merchid']=$goods['merchid'];
			$newgoods['title']=$goods['title'];
			$newgoods['image']=$goods['image'];
			$newgoods['istaocan']=$goods['istaocan']?1:0;
			$newgoods['leixings']=$goods['leixings'];
			$newgoods['price']=$goods['options']['price'];
			$newgoods['optionid']=$goods['options']['id'];
			$newgoods['goodsnum']=$goods['options']['goodsnum'];
			$newgoods['selespename']=$goods['options']['title'];
			$newgoods['total']=1;
			$newgoods['numset']=$numset;
			$allgoods[$key]=$newgoods;
		}
	}else if($type=='plus'){
		if($allgoods[$key]){
			$allgoods[$key]['total']++;
		}else{
			$newgoods['id']=$goods['id'];
			$newgoods['goodsid']=$goods['id'];
			$newgoods['merchid']=$goods['merchid'];
			$newgoods['title']=$goods['title'];
			$newgoods['image']=$goods['image'];
			$newgoods['istaocan']=$goods['istaocan']?1:0;
			$newgoods['leixings']=$goods['leixings'];
			$newgoods['price']=$goods['price'];
			$newgoods['total']=1;
			$newgoods['numset']=$numset;
			$allgoods[$key]=$newgoods;
		}
	}else if($type=='sp_minus' || $type=='minus'){
		if($allgoods[$key]){
			if($allgoods[$key]['total']>1){
				$allgoods[$key]['total']--;
			}else{
				unset($allgoods[$key]);
			}
		}
	}
	$goodsprice=0;
	foreach($allgoods as $av){
		$goodsprice +=$av['price']*$av['total'];
	}
	$this->session('upgoods',$allgoods);
	$res['allgoods']=$allgoods;
	$res['goodsprice']=$goodsprice;
	exi($res);
}
if($_RQ['op']=='postgoods'){
	$post=get_postdata();
	$goods=$post;
	$peisetgoods=$this->session('peisetgoods')?:array();
	$key=$goods['goodsid'].'_'.$goods['optionid'];
	$peisetgoods[$key]=$goods;
	$this->session('peisetgoods',$peisetgoods);
	exi('提交成功');
}
if($_RQ['op']=='time_days'){
		$year =$_RQ['year']?:date('Y');
		$month = $_RQ['month']?:date('m');
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$calendar = array();
		$i = 1;
		$goods=$this->session('peisetgoods');
		$goodsnum=array();
		foreach($goods as $k=>$gv){
			$goodsnum[$k]=$gv['goodsnum']?:$gv['total'];
		}
		while ($i <= $day) {
				$key=$i;
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date=$year . '-' . $month . '-' . $i;
				if(strtotime($date)<time()){
					$islast=1;
				}else{
					$islast=0;
				}
				$isgoods=0;
				if(!$islast){
					$week = date('w', strtotime($date));
					if($goods){
						$daygoods=array();
						foreach($goods as $key=>&$gv){
							//商品
							if($goodsnum[$k]<=0){
								continue;
							}
							foreach($gv['numset'] as $lkey=>$nv){
								$data=$nv['data'];
								if($goodsnum[$k]<=0){
									break;
								}
								if($data[$week]['num']>0){
									$gv['newtitle']=$gv['title'];
									if($nv['title'] || $gv['selespename']){
										$gv['newtitle']=$nv['title'].'-'.$gv['selespename'];
									}
									$gv['newtitle']=cfc('tools')->substr($gv['newtitle'],0,5);
									if($data[$week]['num']>$goodsnum[$k]){
										$data[$week]['num']=$goodsnum[$k];
									}
									$gv['num']=$data[$week]['num'];
									$goodsnum[$k]-=$data[$week]['num'];
									$isgoods=$islast?0:1;
									$daygoods[]=$gv;
								}else{
									continue;
								}
							}
						}
					}
				}
				$calendar[] = array(
					'i'=>$i,
					'day'=>$date,
					'islast'=>$islast,
					'isgoods'=>$isgoods,
					'goods'=>$daygoods
				);
				++$i;
		}
		$return['calendar'] = $calendar;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $year + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['months']=$months;
		$return['years']=$years;
		$return['year']=$year;
		$return['month']=$month;
		exi($return);
}
?>